﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HostServicePhoneCommands.Properties;

namespace HostServicePhoneCommands
{
    public partial class DeleteSnapshotHyperV : Form
    {
        public string NameMachine { get; set; }

        public DeleteSnapshotHyperV()
        {
            InitializeComponent();
        }

        private void DeleteSnapshotHyperVShown(object sender, EventArgs e)
        {
            LblVmMachine.Text = NameMachine;
            var vm = Program.PhoneService.FindVirtualMachine(NameMachine);
            if (vm != null)
            {
                for (var i = 0; i < vm.Snapshots.Count(); i++)
                {
                    ChkLstSnapshots.Items.Add(vm.Snapshots[i]);
                }
            }
        }

        private void BntOkClick(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            foreach (var cmd in ChkLstSnapshots.CheckedItems.Cast<object>().Select(item => Program.PhoneService.DeleteSnapshot(NameMachine, item.ToString())).Where(cmd => !cmd.Successful))
            {
                Cursor = Cursors.Default;
                MessageBox.Show(cmd.ErrorMessage, Resources.NameApp, MessageBoxButtons.OK,MessageBoxIcon.Error);
                return;
            }
            Cursor = Cursors.Default;
            Close();
        }
    }
}
